package com.landeb.shop.huo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.landeb.shop.R;
import com.landeb.shop.huo.bean.BeanFragProducts;
import com.landeb.shop.huo.bean.BeanProductFragRightGv;

import java.util.List;

/**
 * Created by blade on 2016/10/21.
 */

public class FragProductsGVAdapter extends BaseAdapter {

    //    private List<ItemFragProductsGridView> mList;
    private LayoutInflater mInflater;
    private Context mContext;
    private List<BeanProductFragRightGv.DataBean.ListBean> topGvList;

//    public FragProductsGVAdapter(Context context, List<ItemFragProductsGridView> list) {
//        mContext = context;
//        mList = list;
//        mInflater = LayoutInflater.from(context);
//    }


    public FragProductsGVAdapter(Context context, List<BeanProductFragRightGv.DataBean.ListBean> topGvList) {
        mContext = context;
        this.topGvList = topGvList;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return topGvList.size();
    }

    @Override
    public Object getItem(int position) {
        return topGvList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.item_frag_product_gv, parent, false);

            holder = new ViewHolder();

            holder.mImageView = (ImageView) convertView.findViewById(R.id.imgV_item_frag_product_gridView);
            holder.mTitle = (TextView) convertView.findViewById(R.id.title_item_frag_product_gridView);
            holder.mPrice = (TextView) convertView.findViewById(R.id.price_item_frag_product_gridView);
            holder.mSaleCount = (TextView) convertView.findViewById(R.id.sale_count_item_frag_product_gridView);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


// TODO: 2016/10/15 用glide加载图片并赋值给      mImageView
        String title = topGvList.get(position).getName();
        holder.mTitle.setText(title);
        String price = topGvList.get(position).getPrice();
        holder.mPrice.setText("¥" + price);
        holder.mSaleCount.setText("销量："+topGvList.get(position).getCount_sale());
        String picUrl = topGvList.get(position).getPic();
        Glide
                .with(mContext)
                .load(picUrl)
                .placeholder(R.drawable.error_pic)
                .error(R.drawable.error_pic)
                .crossFade()
                .into(holder.mImageView);
//        Glide.with(mContext).load(R.mipmap.ic_launcher).into(holder.mImageView);
        return convertView;
    }

    private class ViewHolder {
        private ImageView mImageView; //图标
        private TextView mTitle; //文字
        private TextView mPrice;
        private TextView mSaleCount;
    }


}
